﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simulate_control.Basic
{
    #region USING
    using simulate_control.Repositories;
    using simulate_control.Models;
    using simulate_control.Helpers;
    #endregion

    public partial class FrmDimmerAndContrast : Form
    {
        #region VARIABLES
        public EventHandler Handler { get; set; }
        #endregion

        #region METHODS
        public FrmDimmerAndContrast()
        {
            InitializeComponent();
        }

        public void SetUpProgBars()
        {
            prgBarDimmer.Maximum = 8;
            prgBarDimmer.Minimum = 1;
            prgBarDimmer.Step = 1;
            prgBarDimmer.Value = 1;

            prgBarContrast.Maximum = 63;
            prgBarContrast.Minimum = 0;
            prgBarContrast.Step = 1;
            prgBarContrast.Value = 0;
        }

        public void ProcessKeyEvent(KEY keyEvent)
        {
            switch (keyEvent)
            {
                case KEY.ENTER:
                    // Store data
                    var lst = DimmerAndContrastRepository.SelectAll();
                    DimmerAndContrast item = null;
                    if (lst.Count > 0)
                    {
                        item = lst[0];
                        item.Dimmer = prgBarDimmer.Value;
                        item.Contrast = prgBarContrast.Value;
                        DimmerAndContrastRepository.Save();
                    }
                    else
                    {
                        item = new DimmerAndContrast
                        {
                            Dimmer = prgBarDimmer.Value,
                            Contrast = prgBarContrast.Value
                        };
                        DimmerAndContrastRepository.Insert(item);
                    }
                    this.Handler(this, null);
                    break;
                case KEY.UP:
                    if (prgBarDimmer.Value < prgBarDimmer.Maximum)
                    {
                        prgBarDimmer.Value++;
                        lblDimmer.Text = prgBarDimmer.Value.ToString();
                    }
                    break;
                case KEY.DOWN:
                    if (prgBarDimmer.Value > prgBarDimmer.Minimum)
                    {
                        prgBarDimmer.Value--;
                        lblDimmer.Text = prgBarDimmer.Value.ToString();
                    }
                    break;
                case KEY.RIGHT:
                    if (prgBarContrast.Value < prgBarContrast.Maximum)
                    {
                        prgBarContrast.Value++;
                        lblContrast.Text = prgBarContrast.Value.ToString();
                    }
                    break;
                case KEY.LEFT:
                    if (prgBarContrast.Value > prgBarContrast.Minimum)
                    {
                        prgBarContrast.Value--;
                        lblContrast.Text = prgBarContrast.Value.ToString();
                    }
                    break;
            }
        }

        private void FrmDimmerAndContrast_Load(object sender, EventArgs e)
        {
            SetUpProgBars();
            LoadData();
        }

        public void LoadData()
        {
            var lst = DimmerAndContrastRepository.SelectAll();
            if (lst.Count > 0)
            {
                prgBarDimmer.Value = lst[0].Dimmer.Value;
                prgBarContrast.Value = lst[0].Contrast.Value;
                lblDimmer.Text = prgBarDimmer.Value.ToString();
                lblContrast.Text = prgBarContrast.Value.ToString();
            }
        }
        #endregion
    }
}
